<template>
  <div class="tab-bar-item" @click="itemClick">
    <div v-if="!isAcitve">
      <slot name="item-icon"></slot>
    </div>
    <div v-else><slot name="item-icon-active"></slot></div>
    <div :style="activeStyle"><slot name="item-text"></slot></div>
  </div>
</template>

<script>
export default {
  props: {
    link: String,
    activeColor: {
      type: String,
      default: "red"
    }
  },
  data() {
    return {};
  },
  computed: {
    // 判断当前活跃路径
    isAcitve() {
      return this.$route.path.indexOf(this.link) != -1;
    },
    // 传入活跃时文字颜色
    activeStyle() {
      return this.isAcitve ? { color: this.activeColor } : {};
    }
  },
  methods: {
    // 跳转至传入的link的路径
    itemClick() {
      this.$router.replace(this.link).catch(err => err);
    }
  }
};
</script>

<style lang="scss" scopd>
.tab-bar-item {
  flex: 1;
  height: 49px;
  text-align: center;
  font-size: 14px;
}

.tab-bar-item img {
  width: 24px;
  height: 24px;
  margin-top: 3px;
  margin-bottom: 2px;
  vertical-align: middle;
}
</style>
